package
{
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.sampler.NewObjectSample;
	import gl.gestures.GestureFlick;
	import gl.touches.TouchDown;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import XML;
	import flash.net.URLLoader;
	import flash.display.MovieClip;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.net.navigateToURL;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import com.greensock.*;
	import com.greensock.easing.*
	import id.core.Application;
	import id.core.TouchSprite;
	import gl.events.TouchEvent;
	import gl.events.GestureEvent
	
	/**
	 * ...
	 * @author Aydarka
	 */
	public class Main extends Application
	{
		private var filename_list:Array = new Array();
		private var url_list:Array = new Array();
		private var url_target_list:Array = new Array();
		private var title_list:Array = new Array();
		private var description_list:Array = new Array();
		
		private var i:Number;
		private var tn:Number = 0;
		private var scale_factor:Number = 0.6;
		private var tween_duration:Number = 0.8;
		
		private var angle:Number;
		private var degree:Number
		private var total:Number;
		private var totalPic:Number;
		private var flashmo_xml:XML = new XML();
		private var folder:String = "thumbnails/";
		private var xml_loader:URLLoader = new URLLoader();
		private var thumbnail_group:TouchSprite;
		private var default_y:Number;
		private var flashmo_mc:TouchSprite;
		private var pic_loader:Loader;
		private var loaderName:String;
		private var bigpicLoader:Loader;
		private var loaderStats:TouchEvent;
		private var Xint:Number;
		private var FlickCheker:Number = 0;
		private var Cicl:Number = 0;
		private var NextEvent:GestureEvent;
		var tweenNext:TweenLite;
		
		public function Main():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var tn_group:tngroup = new tngroup;
			var fm_previous:fm = new fm;
			var fm_next:fm = new fm;
			
			tn_group.x = 0;
			tn_group.y = 300;
			
			fm_previous.x = 30;
			fm_previous.y = 283;
			
			fm_next.x = 30;
			fm_next.y = 317;
			
			addChild(fm_previous);
			addChild(tn_group);
			addChild(fm_next);
			
			var tn_title:TextField = new TextField;
			var tn_desc:TextField = new TextField;
			var tn_url:TextField = new TextField;
			
			tn_title.x = 312;
			tn_title.y = 252;
			tn_title.width = 409
			tn_title.height = 21
			
			tn_desc.x = 312;
			tn_desc.y = 298;
			tn_desc.width = 409
			tn_desc.height = 50
			
			tn_url.x = 312;
			tn_url.y = 275;
			tn_url.width = 409
			tn_url.height = 21
			
			addChild(tn_title);
			addChild(tn_desc);
			addChild(tn_url);
			xml_loader.load(new URLRequest("flashmo_129_thumbnail_list.xml"));
			xml_loader.addEventListener(Event.COMPLETE, create_thumbnail);
			thumbnail_group = new TouchSprite()
			this.addChild(thumbnail_group);
			
			thumbnail_group.x = 512
			 thumbnail_group.y = 300;
			
			tn_group.visible = false;
			fm_previous.visible = false;
			fm_next.visible = false;
			
			tn_title.text = "";
			tn_desc.text = "";
			tn_url.text = "";
			
			this.addEventListener(GestureEvent.GESTURE_FLICK_1, to_next);
			addEventListener(TouchEvent.TOUCH_MOVE, rotate);
			
		
		}
		
		private function create_thumbnail(e:Event):void
		{
			flashmo_xml = XML(e.target.data);
			total = flashmo_xml.thumbnail.length();
			totalPic = 20
			angle = Math.PI*2 / totalPic
			degree = Math.round(angle * 180 / Math.PI);
			
			for (i = 0; i < total; i++)
			{
				filename_list.push(flashmo_xml.thumbnail[i].@filename.toString());
				url_list.push(flashmo_xml.thumbnail[i].@url.toString());
				url_target_list.push(flashmo_xml.thumbnail[i].@target.toString());
				title_list.push(flashmo_xml.thumbnail[i].@title.toString());
				description_list.push(flashmo_xml.thumbnail[i].@description.toString());
			}
			load_tn();
		}
		
		private function load_tn():void
		{
			var pic_request:URLRequest = new URLRequest(folder + filename_list[tn]);
			pic_loader = new Loader();
			
			pic_loader.load(pic_request);
			pic_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loaded);
			tn++;
		
		}
		
		private function on_loaded(e:Event):void
		{
			if (tn < totalPic)
			{
				load_tn();
			}
			else
			{
				
					//fm_previous.addEventListener(TouchEvent.TOUCH_TAP, to_previous);
				
					//stage.addEventListener(MouseEvent.MOUSE_WHEEL, on_wheel);
			}
			
			var flashmo_bm:Bitmap = new Bitmap();
			
			flashmo_mc = new TouchSprite();
			
			flashmo_bm = Bitmap(e.target.content);
			flashmo_bm.x = -flashmo_bm.width * 0.5;
			flashmo_bm.y = -flashmo_bm.height * 0.5;
			flashmo_bm.smoothing = true;
			
			var bg_width = flashmo_bm.width + 10;
			var bg_height = flashmo_bm.height + 10;
			
			flashmo_mc.addChild(flashmo_bm);
			flashmo_mc.graphics.beginFill(0x666666);
			flashmo_mc.graphics.drawRect(-bg_width * 0.51, -bg_height * 0.51, bg_width * 1.02, bg_height * 1.02);
			flashmo_mc.graphics.beginFill(0xFFFFFF);
			flashmo_mc.graphics.drawRect(-bg_width * 0.5, -bg_height * 0.5, bg_width, bg_height);
			flashmo_mc.graphics.endFill();
			var loaderInfo:LoaderInfo = LoaderInfo(e.target.loader.contentLoaderInfo)
			loaderName = loaderInfo.url.substr(16, loaderInfo.url.length);
			flashmo_mc.name = loaderName;
			
			flashmo_mc.buttonMode = true;
			
			//flashmo_mc.addEventListener(TouchEvent.TOUCH_DOWN, tn_over);
			//flashmo_mc.addEventListener(TouchEvent.TOUCH_OUT, tn_out);
			flashmo_mc.addEventListener(TouchEvent.TOUCH_TAP, tn_click);
			flashmo_mc.scaleX = flashmo_mc.scaleY = scale_factor;
			
			flashmo_mc.x = Math.cos(thumbnail_group.numChildren * angle) * 200;
			flashmo_mc.y = Math.sin(thumbnail_group.numChildren * angle) * 200;
			flashmo_mc.rotation = thumbnail_group.numChildren * degree;
			
			thumbnail_group.addChild(flashmo_mc);
			
			Xint = thumbnail_group.numChildren;
		}
		
		private function tn_over(e:TouchEvent):void
		{
		
			//var mc:TouchSprite = e.target as TouchSprite
			//var s_no:Number = parseInt(mc.name.slice(8, 10));
		
			//thumbnail_group.addChild(mc);
		
			//new Tween(mc, "scaleX", Elastic.easeOut, mc.scaleX, 1, tween_duration, true);
			//new Tween(mc, "scaleY", Elastic.easeOut, mc.scaleY, 1, tween_duration, true);
			//TweenLite.to(mc, 1, {scaleX: 1, ease: Elastic.easeOut});
			//	TweenLite.to(mc, 1, {scaleY: 1, ease: Elastic.easeOut});
		
			//tn_title.text = title_list[s_no];
			//tn_desc.text = description_list[s_no];
			//tn_url.text = url_list[s_no];
		}
		
		private function tn_out(e:TouchEvent):void
		{
			var mc:TouchSprite = e.target as TouchSprite
			
			//new Tween(mc, "scaleX", Strong.easeOut, mc.scaleX, scale_factor, tween_duration, true);
			//new Tween(mc, "scaleY", Strong.easeOut, mc.scaleY, scale_factor, tween_duration, true);
			TweenLite.to(mc, 1, {scaleX: scale_factor, ease: Elastic.easeOut});
			TweenLite.to(mc, 1, {scaleY: scale_factor, ease: Elastic.easeOut});
		
		}
		
		private function tn_click(e:TouchEvent):void
		{
			
			var mc:TouchSprite = new TouchSprite()
			mc = e.target as TouchSprite
			trace(e.target.name);
			loaderStats = e
			bigpicLoader = new Loader();
			var URL:URLRequest = new URLRequest("bigpics/" + e.target.name);
			bigpicLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, bigpicLoaded);
			bigpicLoader.load(URL);
		
		}
		
		function bigpicLoaded(en:Event):void
		{
			
			var bim:Bitmap = Bitmap(en.target.content);
			bim.x = loaderStats.stageX;
			bim.y = loaderStats.stageY;
			bim.rotation = loaderStats.target.rotation
			
			addChild(bim)
			TweenLite.to(bim, 1, {x: bim.x + 20, y: 400, rotation: 0, ease: Back.easeOut});
		}
		
		
		private function to_next(e:GestureEvent):void
		{
		
			
				tweenNext = new TweenLite(thumbnail_group, 1, {rotation: thumbnail_group.rotation - 360, ease: Linear.easeNone, onComplete: NextComplete});
			
			
		
			NextEvent = e;
			NextStep(e);
			this.removeEventListener(GestureEvent.GESTURE_FLICK_1, to_next);
		}
		
		private function NextStep(e:GestureEvent):void
		{
			if (NextEvent.velocityX>0) 
			{
					var Obj:TouchSprite = thumbnail_group.getChildAt(thumbnail_group.numChildren - 1) as TouchSprite;
			
			var nextIndex:Number = (filename_list.indexOf(Obj.name)) + 1;
			}
			else 
			{
				
					var Obj:TouchSprite = thumbnail_group.getChildAt(thumbnail_group.numChildren-14) as TouchSprite;
				trace(Obj.name);
				var nextIndex:Number = (filename_list.indexOf(Obj.name)) - 1;
				
				
				
				
			}
		
			
			
			var pic_requeste:URLRequest = new URLRequest(folder + filename_list[nextIndex]);
			var pic_loadere:Loader = new Loader();
			if (NextEvent.velocityX>0) 
			{
			pic_loadere.load(pic_requeste);
			pic_loadere.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loadedUpgradeNext);
			FlickCheker = mouseX;
			}
		}
		
		private function NextComplete():void
		{
			Cicl = 0;
			
			this.addEventListener(GestureEvent.GESTURE_FLICK_1, to_next);
		}
		
		private function on_loadedUpgradeNext(e:Event):void
		{
			
			
			flashmo_mc = drawLoader(e);
			
			if (NextEvent.velocityX>0) 
			{
				flashmo_mc.addEventListener(TouchEvent.TOUCH_TAP, tn_click);
			flashmo_mc.scaleX = flashmo_mc.scaleY = scale_factor;
			
			flashmo_mc.x = (Math.cos(Xint * angle)) * 200;
			flashmo_mc.y = (Math.sin(Xint * angle)) * 200;
			flashmo_mc.rotation = (Xint) * degree;
			thumbnail_group.addChild(flashmo_mc);
			thumbnail_group.removeChildAt(0)
			Xint++
			
			if (Cicl < 5)
			{
				Cicl = Cicl + 1;
				
				NextStep(NextEvent);
			}
			else
			{
				Cicl = 0;
			}
			}
			else 
			{
				flashmo_mc.addEventListener(TouchEvent.TOUCH_TAP, tn_click);
			flashmo_mc.scaleX = flashmo_mc.scaleY = scale_factor;
			
			flashmo_mc.x = (Math.cos(0 * angle)) * 200;
			flashmo_mc.y = (Math.sin(0 * angle)) * 200;
			flashmo_mc.rotation = (0) * degree;
			thumbnail_group.addChild(flashmo_mc);
			thumbnail_group.removeChildAt(Xint)
			
			
			if (Cicl < 10)
			{
				Cicl = Cicl + 1;
				
				NextStep(NextEvent);
			}
			else
			{
				Cicl = 0;
			}
			}
			
		}
		private function drawLoader(e:Event):TouchSprite
		{
			var flashmo_bm:Bitmap = new Bitmap();
			
			flashmo_mc = new TouchSprite();
			
			flashmo_bm = Bitmap(e.target.content);
			flashmo_bm.x = -flashmo_bm.width * 0.5;
			flashmo_bm.y = -flashmo_bm.height * 0.5;
			flashmo_bm.smoothing = true;
			
			var bg_width = flashmo_bm.width + 10;
			var bg_height = flashmo_bm.height + 10;
			
			flashmo_mc.addChild(flashmo_bm);
			flashmo_mc.graphics.beginFill(0x666666);
			flashmo_mc.graphics.drawRect(-bg_width * 0.51, -bg_height * 0.51, bg_width * 1.02, bg_height * 1.02);
			flashmo_mc.graphics.beginFill(0xFFFFFF);
			flashmo_mc.graphics.drawRect(-bg_width * 0.5, -bg_height * 0.5, bg_width, bg_height);
			flashmo_mc.graphics.endFill();
			var loaderInfo:LoaderInfo = LoaderInfo(e.target.loader.contentLoaderInfo)
			loaderName = loaderInfo.url.substr(16, loaderInfo.url.length);
			flashmo_mc.name = loaderName;
			
			flashmo_mc.buttonMode = true;
			return flashmo_mc;
		}
		private function rotate(e:TouchEvent)
		{
			var rad:Number = Math.sqrt(((e.stageX - 150) * (e.stageX - 150)) + ((e.stageY - 150) * (e.stageY - 150)));
			var proport:Number = rad / 135;
			trace("x: ", e.stageX, " | y: ", e.stageY);
			trace("rad: ", rad);
			trace("pr: ", proport);
			var nav_padX:Number = ((e.stageX - 150) / proport) + 150;
			var nav_padY:Number = ((e.stageY - 150) / proport) + 150;
			var angle:int = Math.atan2(150 - nav_padX, nav_padY - 150) * (180 / Math.PI);
			trace("angle "+angle)
		}
	
	}

}

